package com.example.springbootvue.controller;

import com.example.springbootvue.entity.User;
import com.example.springbootvue.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/test")
@RequiredArgsConstructor
public class TestController {

    private final PasswordEncoder passwordEncoder;
    private final UserService userService;

    /**
     * 获取密码的MD5值
     */
    @GetMapping("/encode")
    public ResponseEntity<?> encodePassword(@RequestParam String password) {
        log.info("测试密码加密: {}", password);
        String encoded = passwordEncoder.encode(password);
        log.info("加密结果(MD5): {}", encoded);

        Map<String, Object> response = new HashMap<>();
        response.put("password", password);
        response.put("encoded", encoded);
        return ResponseEntity.ok(response);
    }

    /**
     * 验证密码是否匹配
     */
    @GetMapping("/verify")
    public ResponseEntity<?> verifyPassword(
            @RequestParam String rawPassword,
            @RequestParam String encodedPassword) {
        log.info("测试密码验证 - 原始密码: {}, 编码后密码: {}", rawPassword, encodedPassword);

        boolean matches = passwordEncoder.matches(rawPassword, encodedPassword);
        log.info("验证结果: {}", matches ? "匹配" : "不匹配");

        Map<String, Object> response = new HashMap<>();
        response.put("rawPassword", rawPassword);
        response.put("encodedPassword", encodedPassword);
        response.put("matches", matches);
        return ResponseEntity.ok(response);
    }

    /**
     * 重置用户密码（用于测试）
     */
    @PostMapping("/reset-password")
    public ResponseEntity<?> resetPassword(
            @RequestParam String username,
            @RequestParam String newPassword) {
        log.info("重置用户 {} 的密码", username);

        User user = userService.findByUsername(username);
        if (user == null) {
            log.warn("用户 {} 不存在", username);
            return ResponseEntity.badRequest().body(Map.of("message", "用户不存在"));
        }

        String encodedPassword = passwordEncoder.encode(newPassword);
        log.info("新密码(MD5): {}", encodedPassword);

        user.setPassword(encodedPassword);
        userService.save(user);

        log.info("用户 {} 密码重置成功", username);
        return ResponseEntity.ok(Map.of("message", "密码重置成功"));
    }

    /**
     * 检查密码编码器类型
     */
    @GetMapping("/encoder-info")
    public ResponseEntity<?> getEncoderInfo() {
        log.info("检查密码编码器信息");

        Map<String, Object> response = new HashMap<>();
        response.put("encoderType", passwordEncoder.getClass().getName());

        // 尝试加密并验证一个测试密码
        String testPassword = "123456";
        String encoded = passwordEncoder.encode(testPassword);
        boolean matches = passwordEncoder.matches(testPassword, encoded);

        response.put("testPassword", testPassword);
        response.put("testEncoded", encoded);
        response.put("testMatches", matches);

        return ResponseEntity.ok(response);
    }
}